<template>
    
  <div class="center">
      
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  
  <el-form-item label="姓名" prop="username">
    <el-input type="text" v-model="ruleForm.username" autocomplete="off" style="width:300px"></el-input>
  </el-form-item>
  <el-form-item label="手机号" prop="phone">
    <el-input v-model.number="ruleForm.phone" style="width:300px"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" autocomplete="off" style="width:300px"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="cle">取消</el-button>
  </el-form-item>
</el-form>
  </div>
</template>

<script>
import axios from "axios"
export default {
     data() {
        var checkPhone = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('手机号不可为空'));
        } else {
          if (value !== '') { 
            var reg=/^1[3456789]\d{9}$/;
            if(!reg.test(value)){
              callback(new Error('请输入有效的手机号码'));
            }
          }
          callback();
        };
        }
        // 姓名
         var checkName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('用户名不可为空'));
        } else {
          if (value !== '') { 
            
          }
          callback();
        };
         }
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
        return {
        ruleForm: {
          pass: '',
          username: '',
          phone: ''
        },
        rules: {
          username: [
            { validator: checkName, trigger: 'blur' }
          ], 
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ], 
          phone: [
            { validator: checkPhone, trigger: 'blur' }
          ]
        }
      }
    },
    
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('添加成功');
          } else {
            alert('失败')
            return false;
          };
        });
        console.log(this.ruleForm.username);
        axios.post("http://localhost:3000/api/v3/addservice",{
          username:this.ruleForm.username,
          phoneNumber:this.ruleForm.phone,
          password:this.ruleForm.pass
          }).then(function(){
            console.log("注册成功")
          })
      },
      cle(){
          this.$emit("cle",false);
      }
      
    }
    }
</script>

<style lang="scss" scoped>
    .center{
        z-index: 1200;
        position: fixed;
        top: 50%;
        left: 20%;
        background: lemonchiffon;
    }
</style>